
#import "UIViewController+extension.h"

@implementation UIViewController (extension)
/// 推出弹框
- (void)alert:(NSString *)title
          msg:(NSString*)msg
         left:(nullable void (^)(void))leftBlock
        right:(nullable void (^)(void))rightBlock
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:LS(title) message:LS(msg) preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:LS(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [alert dismissViewControllerAnimated:YES completion:nil];
        if (leftBlock) {
            leftBlock();
        }
    }];
    
    UIAlertAction *enter = [UIAlertAction actionWithTitle:LS(@"确定") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (rightBlock) {
            rightBlock();
        }
    }];
    
    [alert addAction:cancel];
    [alert addAction:enter];
    [self presentViewController:alert animated:YES completion:nil];
    
}

/// 推出Sheet
- (void)sheet:(NSString *)title
          msg:(NSString *)msg
        sheet:(NSArray *)sheetArray
   sheetBlock:(nullable void (^)(int index, NSString *sheetTitle))sheetBlock
{
    [self sheet:title msg:msg sheet:sheetArray cancel:YES sheetBlock:sheetBlock];
}

- (void)sheet:(NSString *)title
          msg:(NSString *)msg
        sheet:(NSArray *)sheetArray
       cancel:(BOOL)showCancel
   sheetBlock:(nullable void (^)(int index, NSString *sheetTitle))sheetBlock
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title == nil ? LS(title) : nil message:msg == nil ? LS(msg) : nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    for (int i = 0; i<sheetArray.count; i++) {
        NSString *sheetTitle = sheetArray[i];
        //        UIAlertActionStyle style = [sheetTitle containsString:@"删除"] ? UIAlertActionStyleCancel : UIAlertActionStyleDefault;
        UIAlertActionStyle style = UIAlertActionStyleDefault;
        UIAlertAction *action = [UIAlertAction actionWithTitle:LS(sheetTitle) style:style handler:^(UIAlertAction * _Nonnull action) {
            if (sheetBlock) {
                sheetBlock(i,sheetTitle);
            }
        }];
        [alert addAction:action];
    }
    if (showCancel)
    {
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:LS(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            //            if (sheetBlock) {
            //                sheetBlock(-1,@"取消");
            //            }
        }];
        [alert addAction:cancel];
    }
    [self presentViewController:alert animated:YES completion:nil];
}
- (void)ImagePicker:(UIImagePickerControllerSourceType)source{
    dispatch_async(dispatch_get_main_queue(), ^{
        [Toast startHud];
    });
    UIImagePickerController *vc = [UIImagePickerController new];
    vc.delegate = self;
    vc.allowsEditing = YES;
    vc.sourceType = source;
    vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self presentViewController:vc animated:YES completion:^{
        [Toast stopHud];
    }];
}
- (UIView *)firstResponder:(UIView *)view
{
    if (view.isFirstResponder)
    {
        return view;
    }
    for (UIView *subView in view.subviews)
    {
        UIView *responder = [self firstResponder:subView];
        if (responder)
        {
            return responder;
        }
    }
    return nil;
}
@end
